package com.doraro.coal.moudle.purch.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doraro.coal.exception.uncheck.BizException;
import com.doraro.coal.moudle.purch.entity.PurchApply;
import com.doraro.coal.moudle.purch.entity.PurchAudit;
import com.doraro.coal.moudle.purch.enums.PermitStatus;
import com.doraro.coal.moudle.purch.mapper.PurchApplyMapper;
import com.doraro.coal.moudle.purch.service.IPurchApplyService;
import com.doraro.coal.moudle.purch.service.IPurchAuditService;
import com.doraro.coal.vo.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 燃料采购计划申请单 服务实现类
 * </p>
 *
 * @author doraro
 * @since 2019-10-08
 */
@Service
public class PurchApplyServiceImpl extends ServiceImpl<PurchApplyMapper, PurchApply> implements IPurchApplyService {
    @Autowired
    private IPurchAuditService auditService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(PurchApply entity) {
        super.save(entity);
        if (entity.getPermitStatus().equals(PermitStatus.DART)) {
            return true;
        }
        final PurchAudit purchAudit = createPurchAudit(entity);
        return auditService.save(purchAudit);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(PurchApply entity) {
        final PermitStatus status = entity.getPermitStatus();
        if (status == null) {
            throw new BizException("非法的状态");
        }
        super.updateById(entity);
        if (status.equals(PermitStatus.DART)) {
            return true;
        }
        final PurchAudit purchAudit = createPurchAudit(entity);
        return auditService.save(purchAudit);
    }

    private PurchAudit createPurchAudit(PurchApply entity) {
        final PurchAudit purchAudit = new PurchAudit();
        purchAudit.setOperName(entity.getCreateUser());
        purchAudit.setOperDept(entity.getCreateUserOrg());
        purchAudit.setOperAction("创建");
        purchAudit.setRemark("");
        purchAudit.setOperTime(LocalDateTime.now());
        purchAudit.setPurchApplyId(entity.getPurchApplyId());
        return purchAudit;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeStatus(String purchId, String remark, UserDto user, PermitStatus to) {
        final PurchApply purchApply = super.getById(purchId);
        if (purchApply == null) {
            throw new BizException("燃料采购计划申请单不存在");
        }
        final PermitStatus permitStatus = purchApply.getPermitStatus();
        if (!permitStatus.change(to)) {
            throw new BizException("非法的状态");
        }
        super.lambdaUpdate().eq(PurchApply::getPurchApplyId,purchId)
                .set(PurchApply::getPermitStatus,to)
                .update();
        final PurchAudit purchAudit = new PurchAudit();
        purchAudit.setOperName(user.getNickName());
        purchAudit.setOperDept(user.getOrganize().getName());
        purchAudit.setOperAction("审核");
        purchAudit.setRemark(remark);
        purchAudit.setOperTime(LocalDateTime.now());
        purchAudit.setPurchApplyId(purchId);
        auditService.save(purchAudit);
    }
}
